package cn.tedu.hospital.admin.server.patient.controller;

import cn.tedu.hospital.admin.server.common.pojo.vo.PageData;
import cn.tedu.hospital.admin.server.common.web.JsonResult;
import cn.tedu.hospital.admin.server.patient.pojo.param.PatientAddNewParam;
import cn.tedu.hospital.admin.server.patient.pojo.param.PatientUpdateParam;
import cn.tedu.hospital.admin.server.patient.pojo.vo.PatientListItemVO;
import cn.tedu.hospital.admin.server.patient.pojo.vo.PatientStandardVO;
import cn.tedu.hospital.admin.server.patient.service.IPatientService;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Date;

/**
 * 处理患者相关请求的控制器类
 *
 * @author java@tedu.cn
 * @version 1.0
 */
@Slf4j
@RestController
@RequestMapping("/patient")
@Validated
@Api(tags = "3.1. 患者管理-基本信息管理")
public class PatientController {

    @Autowired
    private IPatientService patientService;

    public PatientController() {
        log.debug("创建控制器类对象：ArticleController");
    }

    @PostMapping("/add-new")
   // @PreAuthorize("hasAuthority('/content/article/add-new')")
    @ApiOperation("添加患者")
    @ApiOperationSupport(order = 100)
    public JsonResult addNew(@Valid PatientAddNewParam patientAddNewParam) {
        log.debug("开始处理【发布患者】的请求，参数：{}", patientAddNewParam);
      //  String remoteAddr = request.getRemoteAddr();
        patientService.addNew(patientAddNewParam);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/delete")
   // @PreAuthorize("hasAuthority('/content/article/delete')")
    @ApiOperation("根据ID删除患者")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "患者ID", required = true, dataType = "long")
    })
    public JsonResult delete(@PathVariable @Range(min = 1, message = "请提交有效的患者ID值！") Long id) {
        log.debug("开始处理【根据ID删除患者】的请求，参数：{}", id);
        patientService.delete(id);
        return JsonResult.ok();
    }
    @ApiOperation("修改患者")
    @ApiOperationSupport(order = 300)
    @PostMapping("/{id:[0-9]+}/update/info")
   // @PreAuthorize("hasAuthority('/content/tag/update')")
    public JsonResult updateInfoById(@Validated PatientUpdateParam patientUpdateParam) {
        log.debug("开始处理【修改患者】的请求，参数：{}", patientUpdateParam);
        patientService.updateInfoById(patientUpdateParam);
        return JsonResult.ok();
    }

    @GetMapping("/{id:[0-9]+}")
    //@PreAuthorize("hasAuthority('/content/article/read')")
    @ApiOperation("根据ID查询患者详情")
    @ApiOperationSupport(order = 410)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "患者ID", required = true, dataType = "long")
    })
    public JsonResult getStandardById(
            @PathVariable @Range(min = 1, message = "请提交有效的患者ID值！") Long id) {
        log.debug("开始处理【根据ID查询患者详情】的请求，参数：{}", id);
        PatientStandardVO queryResult = patientService.getStandardById(id);
        return JsonResult.ok(queryResult);
    }

    @GetMapping("/list")
   // @PreAuthorize("hasAuthority('/content/article/read')")
    @ApiOperation("查询患者列表")
    @ApiOperationSupport(order = 420)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", defaultValue = "1", paramType = "query", dataType = "long"),
    })
    public JsonResult list(@Range(min = 1, message = "请提交有效的页码值！") Integer page) {
        log.debug("开始处理【查询患者列表】的请求，页码：{}", page);
        Integer pageNum = page == null ? 1 : page;
        PageData<PatientListItemVO> pageData = patientService.list(pageNum);
        return JsonResult.ok(pageData);
    }


}
